package com.yx.accountBook.user.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yx.accountBook.common.BaseService;
import com.yx.accountBook.common.ConsHint;
import com.yx.accountBook.common.Constant;
import com.yx.accountBook.common.result.JsonPageResult;
import com.yx.accountBook.common.result.JsonResult;
import com.yx.accountBook.user.dao.UserMapper;
import com.yx.accountBook.user.dao.UserSubjectMapper;
import com.yx.accountBook.user.domain.User;
import com.yx.accountBook.user.vo.Profession;
import com.yx.accountBook.user.vo.UserVo;
import org.crazycake.shiro.RedisSessionDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by liukailong on 2017/2/9.
 */
@Service
public class UserService extends BaseService<User> {
    private final static Logger log = LoggerFactory.getLogger(UserService.class);

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserSubjectMapper userSubjectMapper;

    @Resource(name = "userMapper")
    public void setUserMapper(UserMapper userMapper) {
        setMapper(userMapper);
        this.userMapper = userMapper;
    }

    public User findUserByUsername(String username){
        User queryObj = new User();
        queryObj.setAccount(username);
        queryObj.setFlag(Constant.ONE);
        User user = this.userMapper.selectOne(queryObj);
        return user;

    }

    /**
     * 查看用户是否禁用时用
     * @author liukailong
     */
    public User findUserByAccount(String username){
        User queryObj = new User();
        queryObj.setAccount(username);
        User user = this.userMapper.selectOne(queryObj);
        return user;

    }

    /**
     *@Author liukailong
     *@Date 2017/3/6
     *@Description 新增
     */
    @Transactional
    public JsonResult add(UserVo user){
        Example example = new Example(User.class);
        example.createCriteria().andEqualTo("account", user.getAccount());
        List<User> adminList = userMapper.selectByExample(example);
        if (adminList.size() != 0){
            return new JsonResult(false, "用户名已存在!");
        }
        userMapper.insertSelective(user);

        return new JsonResult(true, ConsHint.INSERT_SUCCESS);
    }

    /**
     * 禁用
     * @param
     * @return
     * @author liukailong
     * @date 2017/03/07
     */
    public void stop(User user1) {
        User user = new User();
        user.setFlag(user1.getFlag());

        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",user1.getId());

        this.updateByExampleSelective(user,example);
    }

    /**
     *@Author liukailong
     *@Date 2017/3/6
     *@Description 修改
     */
    @Autowired
    private RedisSessionDAO sessionDAO;
    @Transactional
    public JsonResult edit(UserVo user){
        User oldUser = userMapper.selectByPrimaryKey(user.getId());

        userMapper.updateByPrimaryKeySelective(user);


        return new JsonResult(true,ConsHint.UPDATE_SUCCESS);
    }

    /**
     * @Author liukailong
     * @Date 2017/3/6
     * @Description 列表
     * @params keyword:关键字  pageNo:当前页码  pageSize:每页条数
     */
    public JsonPageResult list(int pageNo, int pageSize, String keyword, int role) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        Example.Criteria orCriteria = example.or();

        if(role != Constant.ZERO){
            criteria.andEqualTo("role",role);
            orCriteria.andEqualTo("role",role);
        }
        if (!StringUtils.isEmpty(keyword)) {
            criteria.andLike("account", "%" + keyword + "%");
            orCriteria.andLike("name", "%" + keyword + "%");
        }
        example.setOrderByClause("createTime desc ");
        Page page = PageHelper.startPage(pageNo,pageSize,true,false);
        List<User> list = userMapper.selectByExample(example);
        if(list != null && list.size() > 0){
            List<UserVo> userVos = new ArrayList<UserVo>();
            for(int i=0;i<list.size();i++){
                User user = list.get(i);
                UserVo userVo = convertUser(user);
                //专业信息列表
                String professionId = user.getProfessionId();
                if(!StringUtils.isEmpty(professionId)){
                    List<Profession> professions = getProfessions(professionId);
                    userVo.setProfessions(professions);
                }

                userVos.add(userVo);
            }
            JsonPageResult jsonResultPage = new JsonPageResult(userVos,page);
            return jsonResultPage;
        }else {
            JsonPageResult jsonResultPage = new JsonPageResult(list,page);
            return jsonResultPage;
        }

    }

    private UserVo convertUser(User user){
        UserVo userVo = new UserVo();
        userVo.setId(user.getId());
        userVo.setAccount(user.getAccount());
        userVo.setName(user.getName());
        userVo.setPassword(user.getPassword());
        userVo.setRole(user.getRole());
        userVo.setTel(user.getTel());
        userVo.setFlag(user.getFlag());
        userVo.setCreateTime(user.getCreateTime());
        userVo.setUserPhoto(user.getUserPhoto());
        userVo.setRemark(user.getRemark());
        return userVo;
    }


    /**
     *@Author liukailong
     *@Date 2017/3/6
     *@Description 明细
     *@params id
     */
    @Transactional
    public JsonResult getUserById(long id) {
        /*查询用户*/
        User user = selectByPrimaryKey(id);

        if (user== null){
            return new JsonResult(false,"信息不存在");
        }
        UserVo userVo = convertUser(user);

        //专业信息列表
        String professionId = user.getProfessionId();
        if(!StringUtils.isEmpty(professionId)){
            List<Profession> professions = getProfessions(professionId);
            userVo.setProfessions(professions);
        }
        return new JsonResult(userVo);
    }

    /**
     * 由专业类ID查找专业名称列表
     * @param professionIds
     * @return
     */
    private List<Profession> getProfessions(String professionIds) {

        List<Profession> professions = new ArrayList<>();

        return professions;
    }

}
